Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

177
Visualizações
JavaScript Question - Is there any way to assign an object to "this" keyword without using the call, apply and bind methods?

I am trying to create a polyfill for the call, apply and bind methods.

const user = {
  firstName: "Christopher",
  lastName: "Nolan",
};

const fullName = function (place, country) {
  console.log(
    `${this.firstName} ${this.lastName} is from ${place}, ${country}.`
  );
};

// Using a call method

fullName.call(user, "London", "UK");

// Re-creating a call method with the name of "_call"

Function.prototype._call = function (...args) {
  const funcObj = this;
  const params = args.slice(1);
  return (function () {
    const obj = args[0];
       
    // How can I point out the obj to "this" keyword inside a funcObj without using the bind method as I mentioned below.
         return funcObj.bind(obj, ...params)();
         })();
};

fullName._call(user, "London", "UK");
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Yes. Just use

var myObject = this;

I understand the problem you might be having. Sometimes by the time you wish to use the 'this' object the context of 'this' has changed. Some people use

var that = this;

Here's some more reading for you that goes a bit deeper.

https://dbwriteups.wordpress.com/2017/04/08/what-does-that-this-in-javascript-mean/

about 4 years ago · Juan Pablo Isaza Relatório

0

I'm not sure this is a great implementation, but you could take advantage of the fact that for an object method the scope is automatically set to the object upon which it was invoked.

With that in mind you could create an object using the scope as the prototype and add the original function as a method, then invoke the method.

const user = {
  firstName: "Christopher",
  lastName: "Nolan",
};

const fullName = function (place, country) {
  console.log(
    `${this.firstName} ${this.lastName} is from ${place}, ${country}.`
  );
};

Function.prototype._call = function (scope, ...args) {
  // symbol for the method name to avoid name collisions
  const symbol = Symbol();
  
  // create a new object from scope with the original function (this) as a method
  const temp = Object.create(scope, {[symbol]: { value: this }});
  
  // inside the method "this" will point to "temp" which is (effectively) "scope"
  return temp[symbol](...args);
}

fullName._call(user, 'London', 'UK');

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda